package com.hlkj.warboot.module_sys.controller;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hlkj.warboot.configurer.util.LoggerUtil;
import com.hlkj.warboot.configurer.util.ResponseUtil;
import com.hlkj.warboot.configurer.util.StrUtil;
import com.hlkj.warboot.module_sys.entity.LogEntity;
import com.hlkj.warboot.module_sys.entity.wrapper.LogWrapper;
import com.hlkj.warboot.module_sys.service.LogService;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.text.MessageFormat;
import java.util.Arrays;

/**
 * <p>
 * 系统日志 前端控制器
 * </p>
 *
 * @author lixing_java@163.com
 * @since 2020-06-11
 */
@Controller
@RequestMapping("/sys/log")
public class LogControler {
    @Resource
    private LogService logService;

    /*
     * @Description TODO 分页
     * @Author 李星
     * @Date 2019/2/16 13:08
     * @Param []
     * @return org.springframework.web.servlet.ModelAndView
     */
    @GetMapping("toPage")
    public ModelAndView toPage() throws Exception {
        return new ModelAndView( "sys/log/logList" );
    }
    @PostMapping("page")
    @ResponseBody
    public Object page(LogEntity entity) throws Exception {
        LoggerUtil.info(MessageFormat.format("【日志分页】条件：{0}", JSON.toJSONString(entity)));

        IPage<LogEntity> iPage = logService.page(new Page<>(entity.basePageNum,entity.basePageSize), LogWrapper.queryWrapper(entity));

        LoggerUtil.info(MessageFormat.format("【日志分页】结果：{0}", iPage.getSize()));
        return  ResponseUtil.success(iPage);
    }

    /*
     * @Description TODO 删除
     * @Author 李星
     * @Date 2019/9/11 9:14
     * @Param [entity]
     * @return java.lang.Object
     */
    @PostMapping("delete")
    @ResponseBody
    public Object delete(LogEntity entity) throws Exception {
        LoggerUtil.info(MessageFormat.format("【日志删除】条件：{0}", entity.getId()));

        boolean flag;
        if(StrUtil.isEffective(entity.getId())) {
            if (entity.getId().contains(",")) {
                flag = logService.removeByIds(Arrays.asList(entity.getId().split(",")));
            } else {
                flag = logService.removeById(entity.getId());
            }
            LoggerUtil.info(MessageFormat.format("【日志删除】结果：{0}", flag));
            return ResponseUtil.success(flag);
        } else {
            return ResponseUtil.fail("主键缺失");
        }
    }

    /*
     * @Description TODO 详情
     * @Author 李星
     * @Date 2019/9/11 9:14
     * @Param [id]
     * @return java.lang.Object
     */
    @GetMapping("detail/{id}")
    public ModelAndView detail(@PathVariable String id) throws Exception {
        LoggerUtil.info(MessageFormat.format("【日志详情】条件：{0}", id));

        LogEntity logEntity = logService.getById(id);

        LoggerUtil.info(MessageFormat.format("【日志详情】结果：{0}", JSONObject.toJSONString(logEntity)));

        ModelAndView modelAndView = new ModelAndView( "sys/log/logDetail" );
        modelAndView.addObject("entity",logEntity);
        return modelAndView;
    }
}

